<?php

namespace plugin\admin\app\controller\auth;

use plugin\admin\app\common\xbuilder\XBuilderForm;
use plugin\admin\app\common\xbuilder\XBuilderList;
use plugin\admin\app\controller\Base;
use plugin\admin\app\controller\common\Crud;
use plugin\admin\app\model\AdminRoleThink as ModelAdminRole;
use plugin\admin\app\model\AdminRuleThink;
use plugin\admin\app\Util;

/**
 * 管理员角色设置
 */
class AdminRole extends Base
{
    /**
     * @var AdminRole
     */
    protected $model = null;

    /**
     * 增删改查
     */
    use Crud;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new ModelAdminRole;
    }
    /**
     */
    public function index()
    {
        $builder = new XBuilderList();
        return $builder->setMetaTitle('角色配置')
            ->addTopButton('add',$this->add())
            ->addTopButton('forbid')
            ->addTopButton('recycle')
            ->addTopButton('restore')
            ->addTopButton('delete')
            ->addTableColumn('id', 'ID','',false,true)
            ->addTableColumn('name', '用户名')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('created_at', '创建时间', 'datetime')
            ->addRightButton('edit',$this->edit()) // 添加编辑弹窗按钮
            ->addRightButton('forbid') // 添加编辑按钮
            ->addRightButton('resume') // 添加编辑按钮
            ->addRightButton('recycle') // 添加编辑按钮
            ->addRightButton('restore') // 添加编辑按钮
            ->addRightButton('delete') // 添加编辑按钮
            ->setTableApi('/app/admin/auth/adminrole/listApi')
            ->getPageData();
    }
    public function listApi()
    {
        $orderBy = request()->input('orderBy', 'updated_at', 'string') ?: 'updated_at';
        $orderDir = request()->input('orderDir', 'desc', 'string') ?: 'desc';
        $orderArr = [
            'id' => 'desc',
            $orderBy => $orderDir
        ];
        $map[] = ['id','<>',1];
        $map[] = ['status','>','-1'];
        $data_list = $this->model
            ->where($map)
            ->order($orderArr)
            ->paginate();
        $get_table_data = XBuilderList::handleTableData($data_list);
        return $this->resSucess('请求成功',$get_table_data);
    }
    public function add()
    {
        return $this->form('add');
    }
    public function edit()
    {
        return $this->form('edit');
    }

    protected function form($type = 'add')
    {
        if (request()->method() == 'POST') {
            $slider_object = $this->model;
            $data          = request()->except(['create_at']);
            if($type === 'add'){
                $result=$slider_object->save($data);
            } else {
                if($data['id']==1){
                   return $this->resError('更新失败，超级管理员禁止修改权限');
                }
                $result=$slider_object->update($data);
            }
            if ($result) {
                return $this->resSucess('操作成功');
            } else {
                return $this->resError('操作失败');
            }
        } else {
            $statusOpt = [  //单选
                'type'=>'select',
                'options'=>[
                    ['label'=>'未审核', 'value'=>'0'], ['label'=>'驳回', 'value'=>'-1'], ['label'=>'审核', 'value'=>'1']
                ]
            ];
            $AdminRuleThink = new AdminRuleThink;
            $data = $AdminRuleThink->select();
            $treeData = [
                'type'=>'input-tree',
                'name'=>'rules',
                'label'=>'权限',
                'multiple'=>true,
                'withChildren'=>true,
                'autoCheckChildren'=>false,
                'options'=>$this->getFormatTree($data)
            ];
            // 使用FormBuilder快速建立表单页面
            $builder = new XBuilderForm();
            return $builder->setMetaTitle('新增角色') // 设置页面标题
            ->setFormUrl($type) // 设置表单提交地址
            ->setFormMode('horizontal')
            ->addFormItem('name', 'text', '角色名称', true)
                ->addFormItem('rules', $treeData, '权限', true)
                ->addFormItem('status', $statusOpt,'状态')
                ->getFormData();
        }
    }

    public function test(){
        $AdminRuleThink = new AdminRuleThink;
        $data = $AdminRuleThink->select();
        return $this->getFormatTree($data);
    }
    /**
     * 删除
     * @param Request $request
     * @return \support\Response
     * @throws \Support\Exception\BusinessException
     */
    public function delete(Request $request)
    {
        $column = $request->post('column');
        $value = $request->post('value');
        $item = $this->model->where($column, $value)->first();
        if (!$item) {
            return $this->json(0);
        }
        if ($item->id == 1) {
            return $this->json(1, '无法删除超级管理员角色');
        }
        $this->model->where('id', $item->id)->delete();
        return $this->json(0);
    }

}
